<template>
  <div class="xtx-form">
    <div class="user-info">
      <img :src="avatar" alt="" />
      <p>Hi，{{ nickname }} 欢迎来小兔鲜，完成绑定后可以QQ账号一键登录哦~</p>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <input
          class="input"
          type="text"
          placeholder="绑定的手机号"
          v-model="form.mobile"
        />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <input
          class="input"
          type="text"
          placeholder="短信验证码"
          v-model="form.code"
        />
        <span class="code" @click="send">{{
          countTime === 0 ? '发送验证码' : `${countTime} s 后重试`
        }}</span>
      </div>
      <div class="error"></div>
    </div>
    <a href="javascript:;" class="submit" @click="bind">立即绑定</a>
  </div>
</template>

<script>
import { reactive, ref } from 'vue-demi'
import QC from 'qc'
import { message } from '@/components/Message'
import { reqUserQQBindCode, reqUserQQBindLogin } from '@/api/user'
import { useCountDown } from '@/compositions'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
export default {
  name: 'CallbackBind',
  props: ['openId'],
  setup(props) {
    const store = useStore()
    const router = useRouter()
    const nickname = ref('')
    const avatar = ref('')

    const form = reactive({
      mobile: '',
      code: ''
    })
    // 获取头像和昵称
    if (QC.Login.check()) {
      QC.api('get_user_info').success((res) => {
        nickname.value = res.data.nickname
        avatar.value = res.data.figureurl_qq_2
      })
    }

    const { countTime, start } = useCountDown()

    const send = async () => {
      // 防止倒计时期间还能重复发送验证码
      if (countTime.value !== 0) return
      if (/^1[3-9]\d{9}$/.test(form.mobile)) {
        // 手机号，发送验证码
        try {
          await reqUserQQBindCode(form.mobile)
          // 发送成功之后，开始倒计时
          start()
        } catch (e) {
          console.log('发送验证码失败')
        }
      } else {
        message({ type: 'error', text: '手机号格式不对' })
      }
    }
    const bind = async () => {
      // 校验手机号和 code
      if (!/^1[3-9]\d{9}$/.test(form.mobile) || form.code.trim() === '') {
        return message({ type: 'error', text: '手机号或验证码不对' })
      }
      // 调用绑定的接口
      const res = await reqUserQQBindLogin({ unionId: props.openId, ...form })
      // 存储信息到 Vuex
      // console.log(res.result)
      store.commit('user/setProfile', res.result)
      // 提示
      message({ type: 'success', text: '绑定成功' })
      // 跳转到首页
      router.push('/')
    }
    return { nickname, avatar, form, send, countTime, bind }
  }
}
</script>

<style scoped lang="less">
.user-info {
  width: 320px;
  height: 70px;
  margin: 0 auto;
  display: flex;
  background: #f2f2f2;
  align-items: center;
  padding: 0 10px;
  margin-bottom: 25px;
  img {
    background: #f2f2f2;
    width: 50px;
    height: 50px;
  }
  p {
    padding-left: 10px;
  }
}
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
